package com.fjwt.gz.service.impl;

import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.db.entity.AuAuditOssEntity;
import com.fjwt.gz.db.entity.OssRecordEntity;
import com.fjwt.gz.db.vo.OssRecordVO;
import com.fjwt.gz.service.mapper.AuAuditOssMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 审批文件oss对应关系表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class AuAuditOssService extends ServiceImpl<AuAuditOssMapper, AuAuditOssEntity> {

    @Autowired
    private OssRecordService ossRecordService;

    /**
     * 添加oss记录
     * @param activityId
     * @param auditRecordsId
     * @param url
     * @param sysUserId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRecord(Long activityId, Long auditRecordsId, String url, Long sysUserId,Integer type) {
        OssRecordEntity record = new OssRecordEntity();
        record.setOssUrl(url);
        record.setCreatedAt(new Date());
        record.setBizType(OssFileConfig.BIZ_TYPE.UPLOAD);
        record.setSysUserId(sysUserId);
        record.setUploadType(Constants.OSS_UPLOAD_TYPE.ACTIVITY_COUPON_SETTING);
        ossRecordService.save(record);

        AuAuditOssEntity entity = new AuAuditOssEntity();
        entity.setAuditRecordsId(auditRecordsId);
        entity.setOssRecordId(record.getOssRecordId());
        entity.setCreatedAt(new Date());
        entity.setInfoId(activityId);
        entity.setFileType(type);
        return save(entity);
    }

    /**
     * 查询活动最后一次驳回之后的审批配置函记录
     */
    public OssRecordVO getLastAuditActivityOss(Long activityId, Integer fileType) {
        return baseMapper.getLastAuditActivityOss(activityId, fileType);
    }
}
